package com.ruoyi.web.controller.module.product;

import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.product.ProductCart;
import com.ruoyi.system.service.module.product.IProductCartService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "购物车控制器", tags = {"购物车管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/product/cart")
public class ProductCartController extends BaseController {

    private final IProductCartService productCartService;

    /**
     * 查询购物车列表
     */
    @ApiOperation("查询购物车列表")
    @PreAuthorize("@ss.hasPermi('module:cart:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductCart> list(@Validated ProductCart productCart) {
        return PageUtils.buildDataInfo(productCartService.page(PageUtils.buildPage()));
    }

    /**
     * 获取购物车详细信息
     */
    @ApiOperation("获取购物车详细信息")
    @PreAuthorize("@ss.hasPermi('module:cart:query')")
    @GetMapping("/{id}")
    public AjaxResult<ProductCart> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(productCartService.getById(id));
    }

    /**
     * 新增购物车
     */
    @ApiOperation("新增购物车")
    @PreAuthorize("@ss.hasPermi('module:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ProductCart productCart) {
        return toAjax(productCartService.save(productCart) ? 1 : 0);
    }

    /**
     * 修改购物车
     */
    @ApiOperation("修改购物车")
    @PreAuthorize("@ss.hasPermi('module:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCart productCart) {
        return toAjax(productCartService.updateById(productCart) ? 1 : 0);
    }

    /**
     * 删除购物车
     */
    @ApiOperation("删除购物车")
    @PreAuthorize("@ss.hasPermi('module:cart:remove')")
    @Log(title = "购物车" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(productCartService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
